home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_166 / autograf / averages.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  327 lines

  1. /*    averages.c
  2.  * program to compute average cost per gallon and miles per gallon from
  3.  *   Superbase exported autolog file
  4.  *    By Joel Swank 8/30/88 
  5.  *      Version 1.1
  6.  */
  7. /*
  8.    Reads a datafile of gasoline purchases of the following format:
  9.    Individual fields separated by commas.
  10.    * date - character format (MMM DD YYYY)
  11.    * odometer reading - 999999.9
  12.    * price per gallon - $9.999
  13.    * Total cost of purchase - $9999.99
  14.    * gallons - 999.9
  15.    * place and brand of gas - character
  16.  
  17.    Execute from cli.
  18.    >averages -?           ; to get options list
  19.  
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <fcntl.h>
  24. #include <ctype.h>
  25. #include <exec/types.h>
  26. #define STDIN 0   /* file descriptor for stdin */
  27.  
  28. char linebuf[100];
  29.  
  30. /* year selection criteria */
  31. int yr = -1;
  32. int eyr = -1;
  33. int syr = -1;
  34.  
  35. /* Min and Max saveareas   */
  36. float maxtank;
  37. float maxcost = 0;
  38. float maxpr = 0;
  39. float maxgal = 0;
  40. float minpr =1e10;
  41. char maxtdate[20];
  42. char maxprdate[20];
  43. char minprdate[20];
  44. char maxcostdate[20];
  45. char maxgaldate[20];
  46. int maxflag = FALSE;
  47.  
  48. FILE *fdopen();
  49.  
  50. int fileflag = FALSE;
  51.  
  52. char date[20];
  53.  
  54. /***********************************************/
  55. /*
  56. /*    Main routine
  57. /*
  58. /***********************************************/
  59.  
  60. main(argc,argv)
  61. int argc;
  62. char *argv[];
  63. {
  64.    FILE *filep;
  65.    int cnt;
  66.  
  67.    if (argc == 0) exit(0); /* can't handle workbench */
  68.  
  69.  
  70.         /* get command line options */
  71.     getcmd(argc, argv);
  72.  
  73.         /* get command line filenames */
  74.     for (cnt=1; cnt < argc; cnt++)
  75.     {    if (*argv[cnt] != '-')
  76.         {
  77.             if ((filep = fopen(argv[cnt], "r")) == NULL)
  78.                 fatal("can't open %s", argv[cnt]) ;
  79.  
  80.             printf("FILE: %s\n",argv[cnt]);
  81.             dofile(filep);
  82.             fclose(filep);
  83.             fileflag = TRUE;
  84.         }
  85.     }
  86.  
  87.     if (!fileflag)  /* if no files given, use stdin */
  88.     {
  89.         filep = fdopen(STDIN, "r");
  90.         dofile(filep);
  91.         fclose(filep);
  92.     }
  93. }
  94.  
  95. /***********************************************/
  96. /*
  97. /*     do a file
  98. /*
  99. /***********************************************/
  100.  
  101.  
  102.  
  103. dofile(fp)
  104. FILE *fp;
  105. {
  106.    char *pp, *tp;
  107.    float firstod = 0, miles, od, atof();
  108.    float lastod, prevod;
  109.    float gallons, cost, price;
  110.    float totgal = 0, totcost = 0;
  111.    int count = 0;
  112.    int year;
  113.  
  114.    maxtank = 0;
  115.    maxcost = 0;
  116.    maxpr = 0;
  117.    maxgal = 0;
  118.    minpr =1e10;
  119.  
  120.    while (NULL != fgets(linebuf,100,fp))
  121.       {
  122.  
  123.       pp = linebuf;
  124.  
  125.       tp = date;
  126.       while (*(pp) != ',') *tp++ = *pp++;    /* save date */
  127.       *tp ='\0';
  128.  
  129.       pp -= 4;
  130.       year = atoi(pp);    /* get year */
  131.       pp += 5;
  132.  
  133.       /* eliminate undesired years */
  134.       if (yr != -1)    
  135.           {
  136.         if (year < yr) continue;
  137.         if (year != yr) break;
  138.         }
  139.  
  140.       if (eyr != -1)    
  141.           {
  142.         if (year > eyr) break;
  143.         }
  144.  
  145.       if (syr != -1)    
  146.           {
  147.         if (year < syr) continue;
  148.         }
  149.  
  150.       od = atof(pp);    /* get odometer reading */
  151.  
  152.       if (!firstod) firstod = od;
  153.       else {
  154.           float t = od-prevod;
  155.         if (t > maxtank) 
  156.             {
  157.             maxtank = t;
  158.             strcpy(maxtdate,date);
  159.             }
  160.         }
  161.  
  162.       lastod = od;
  163.  
  164.       while (*(++pp) != ',') ;
  165.       while (*(++pp) != '$') ;
  166.       pp++;
  167.       price = atof(pp);        /* get price */
  168.  
  169.       while (*(++pp) != ',') ;
  170.       while (*(++pp) != '$') ;
  171.       pp++;
  172.       cost = atof(pp);        /* get cost */
  173.  
  174.       gallons = cost/price;
  175.       /* accumulate data */
  176.       if (count != 0)
  177.           {
  178.          totgal += gallons;
  179.          totcost += cost;
  180.         }
  181.  
  182.       if (maxflag)
  183.           {
  184.         if (price > maxpr) { maxpr = price; strcpy(maxprdate,date); }
  185.         if (price < minpr) { minpr = price; strcpy(minprdate,date); }
  186.         if (gallons > maxgal) { maxgal = gallons; strcpy(maxgaldate,date); }
  187.         if (cost > maxcost) { maxcost = cost; strcpy(maxcostdate,date); }
  188.         }
  189.       count++;
  190.       prevod = od;
  191.       }
  192.  
  193.       /* check for enuf data */
  194.       if (count < 2) 
  195.           {
  196.         printf("No Data\n");
  197.         return;
  198.         }
  199.       /* print results */
  200.       miles = lastod-firstod;
  201.  
  202.       printf("%d transactions processed\n", count);
  203.       if (maxflag)
  204.           {
  205.           printf("Maximum distance between transactions= %-5.1f miles on %s\n",
  206.                 maxtank, maxtdate);
  207.           printf("Maximum gallons = %-5.1f on %s\n", maxgal, maxgaldate);
  208.           printf("Maximum cost = $%-6.2f on %s\n", maxcost, maxcostdate);
  209.           printf("Maximum gasoline price = $%-6.3f on %s\n", maxpr, maxprdate);
  210.           printf("Minimum gasoline price = $%-6.3f on %s\n", minpr, minprdate);
  211.         }
  212.       printf("Total Miles = %-8.1f\n", miles);
  213.       printf("Total Gallons = %-7.1f\n", totgal);
  214.       printf("Total Cost = $%-8.2f\n", totcost);
  215.       printf("Average Cost per Gallon = $%-6.4f\n", totcost/totgal);
  216.       printf("Average Miles per Gallon = %-5.2f\n", miles/totgal);
  217.       printf("Average Cost per Mile = %-5.2f cents\n", totcost/miles*100);
  218. }
  219.  
  220.  
  221.  
  222.  
  223. /***********************************************/
  224. /*
  225. /*  getcmd - get arguments from command line 
  226. /*
  227. /***********************************************/
  228.  
  229. getcmd(argc, argv)
  230. register argc ;
  231. register char    *argv[] ;
  232. {
  233.     register cnt ;
  234.                     /* get command options */
  235.     for (cnt=1; cnt < argc; cnt++)
  236.     {    if (*argv[cnt] == '-')
  237.         {    switch(argv[cnt][1])
  238.             {
  239.                case 's':
  240.                 syr = val_yr(&argv[cnt][2]) ;
  241.                 if (syr == -1)
  242.                     fatal("Bad -s value: %s", argv[cnt]) ;
  243.                 printf("Starting with data for year %4d\n",syr);
  244.                 break ;
  245.  
  246.                case 'e':
  247.                 eyr = val_yr(&argv[cnt][2]) ;
  248.                 if (eyr == -1)
  249.                     fatal("Bad -e value: %s", argv[cnt]) ;
  250.                 printf("Eending with data for year %4d\n",eyr);
  251.                 break ;
  252.  
  253.                case 'y':
  254.                 yr = val_yr(&argv[cnt][2]) ;
  255.                 if (yr == -1)
  256.                     fatal("Bad -y value: %s", argv[cnt]) ;
  257.                 printf("Extracting data for year %4d\n",yr);
  258.                 break ;
  259.  
  260.                case 'm':
  261.                 maxflag = TRUE;
  262.                 break ;
  263.  
  264.                case '?':                    /* help option */
  265.                  usage();
  266.                  printf(" averages Ver 1.1 - calculate auto averages.\n");
  267.                  printf(" Options:\n");
  268.                  printf(" ynn  - for requested year only.\n");
  269.                  printf(" snn  - year to start.\n");
  270.                  printf(" enn  - year to end.\n");
  271.                  printf(" m    - include max and min information.\n");
  272.                  printf(" ?    - display this list.\n");
  273.                 exit(0);
  274.  
  275.                default:
  276.                  usage();
  277.                  exit(0);
  278.             }
  279.         }
  280.     }
  281.  
  282. }
  283.  
  284.  
  285.  
  286. usage()
  287. {
  288. printf("usage:averages [-snn] [-enn] [-ynn] [-m] [-?] [file ...]\n");
  289. }
  290.  
  291.  
  292.  
  293. /*
  294.  *  fatal - print standard error msg and halt process
  295.  */
  296. fatal(ptrn, data1, data2)
  297. register char    *ptrn, *data1, *data2 ;
  298. {
  299.     printf("ERROR: ");
  300.     printf(ptrn, data1, data2) ;
  301.     putchar('\n');
  302.     exit(1);
  303. }
  304.  
  305. /*
  306.  * val_yr : validate a year string gadget contents and return
  307.  *          -1 if invalid or year if valid.
  308.  */
  309.  
  310. val_yr(buffer)
  311. char *buffer;
  312. {
  313.     int yr;
  314.     char *bufp;
  315.     bufp = buffer;
  316.     while (*bufp != '\0')
  317.         {
  318.         if (!isdigit(*bufp)) return -1;
  319.         bufp++;
  320.         }
  321.     yr = atoi(buffer);
  322.     if (yr <100 && yr >= 0) return yr+1900;
  323.     if (yr <2200 && yr >= 1900) return yr;
  324.     return -1;
  325. }
  326.  
  327.